package org.mj.im.common.time;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;

public class SystemClock {
    private static final String THREAD_NAME = "system.clock";
    private static final SystemClock MILLIS_CLOCK = new SystemClock(1L);
    private final long precision;
    private final AtomicLong now;

    public SystemClock(long precision) {
        this.precision = precision;
        now = new AtomicLong(System.currentTimeMillis());
        scheduleClockUpdating();
    }
    public static SystemClock millisClock(){
        return MILLIS_CLOCK;
    }
    private void scheduleClockUpdating(){
        ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor(runnable->{
            Thread thread = new Thread(runnable, THREAD_NAME);
            thread.setDaemon(true);
            return thread;
        });
        executorService.scheduleAtFixedRate(()->now.set(System.currentTimeMillis()),precision,precision, TimeUnit.MILLISECONDS);
    }
    public long now(){
        return now.get();
    }
}
